package com.zmn.plat.admin.controller.common.upload;

import java.io.IOException;

import javax.annotation.Resource;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.logger.LoggerUtil;
import com.zmn.plat.manager.dictionary.ProductDict;
import com.zmn.plat.model.vo.mast.ImageReslutDTO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto.ResultDTO;
import com.zmn.plat.business.interfaces.pic.PicUploadBService;

/**
 * 图片上传
 *
 * @author duanzuocai
 * @version v1.0
 * @since 2018/12/5 14:18
 */
@Controller
@RequestMapping("/common/")
public class PicUploadController {

    private Logger logger = LoggerFactory.getLogger(PicUploadController.class);

    /**
     * 公司法人照片路经
     */
    private static final String PIC_PATH_COMPANY_PERSON = "/compay/personbusiness/";

    /**
     * 描述:  营业执照
     */
    private static final String PIC_PATH_COMPANY_BUSINESS = "/compay/business/";

    @Resource
    PicUploadBService picUploadBService;

    /**
     * 上传公司法人法人身份证图片和营业执照
     *
     * @param file 图片
     * @param
     * @return
     */
    @RequestMapping(value = "company/uploadLegalPersonImg")
    @ResponseBody
    public ImageReslutDTO uploadLegalPersonImg(@RequestParam(value = "file") MultipartFile file,
                                               @RequestParam(value = "timestamp") String timestamp,
                                               @RequestParam(value = "elemId") String elemId,
                                               @RequestParam(value = "type") Integer type) {


        ImageReslutDTO imageReslutDTO = new ImageReslutDTO();
        ImageReslutDTO.Data data = new ImageReslutDTO.Data();
        try {
            String directory = type == 1 ? PIC_PATH_COMPANY_PERSON : PIC_PATH_COMPANY_BUSINESS;
            directory += elemId + "/";
            String name = ((CommonsMultipartFile) file).getFileItem().getName();
            int index = name.lastIndexOf(".");
            if (index == -1) {
                return null;
            }
            String suffix = name.substring(index);
            StringBuilder fileName = new StringBuilder(timestamp).append(suffix);

            ResultDTO resultDTO = picUploadBService.uploadPic(directory, fileName.toString(), file.getInputStream());
            if (resultDTO.getStatusBool()) {
                imageReslutDTO.setCode(GlobalDict.RESULT_SUCCESS);
                imageReslutDTO.setMsg(GlobalConsts.SUCCESS_MSG);
                data.setSrc(picUploadBService.getPicUrl(directory, fileName.toString()));
                imageReslutDTO.setData(data);
            }
            return imageReslutDTO;
        } catch (Exception e) {
            LoggerUtil.error(this.getClass(), e.getMessage(), e);
            return new ImageReslutDTO(GlobalDict.RESULT_FAIL, "文件上传失败", null);
        }
    }

}
